package com.dayuanit.booksystem.net;

import java.io.*;
import java.net.Socket;

/**
 * 图书系统的客户端
 */
public class Client {
    public static void main(String[] args) throws Exception {
        Socket socket = new Socket("127.0.0.1", 9999);

        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));



        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String msg = null;
                    while (null != (msg = bufferedReader.readLine())) {
                        bw.write(msg);
                        bw.newLine();
                        bw.flush();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, "监听键盘输入线程").start();


        System.out.println("-----------服务器端响应的结果-----------");

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                    String result = null;
                    while (null != (result = br.readLine())) {
                        System.out.println("执行结果：" + result);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, "接收服务器返回的结果线程").start();

    }
}
